#lang racket


(require "../base/base.rkt")

(require "domain.rkt"
         "segment.rkt"
         "parametric.rkt"
         "patch.rkt")

(provide cut
         points)


(define (segmentate-domains curve domains)
  (map (cλ make-segment curve) domains))

(define (patch-domains surface domains)
  (map (cλ make-patch surface) domains))

(define (cut parametric n-domains (cut-fn (cut-fn-linear)))
  (let ((domain (parametric-domain parametric)))
    (let ((domains (cut-domain domain n-domains cut-fn)))
      (cond ((t-domain? domain)
             (segmentate-domains parametric domains))
            ((uv-domain? domain)
             (patch-domains parametric domains))
            (else
             (error 'cut "Unhandled domain type"))))))

(define (points parametric
                n-points
                (space-fn parametric-object)
                (t-fn #f)
                (cut-fn #f))
  (let ((domain (parametric-domain parametric))
        (space-fn (space-fn parametric)))
    (cond ((t-domain? domain)
           (let ((t-fn (or t-fn (t-fn-min)))
                 (cut-fn (or cut-fn (cut-fn-linear))))
             (points-domain (parametric-domain parametric) n-points space-fn t-fn cut-fn)))
          ((uv-domain? domain)
           (let ((t-fn (or t-fn (list (t-fn-min) (t-fn-min))))
                 (cut-fn (or cut-fn (list (cut-fn-linear) (cut-fn-linear)))))
             (points-domain (parametric-domain parametric) n-points space-fn t-fn cut-fn)))
          (else
           (error 'points "Unhandled domain type")))))